﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using WBFSe3.Wbfs;

namespace WBFSe3.IO
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public class WbfsPath
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public String Path;
        public String[] Files;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public String this[int index]
        {
            get { return this.Path + this.Files[index]; }
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public int Count
        { 
            get { return this.Files.Length; }
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsPath(String path)
        {
            if (!path.EndsWith("\\"))
                path += '\\';

            if (path.IndexOf('\\') != path.LastIndexOf('\\'))
                path = System.IO.Path.GetDirectoryName(path);

            try
            {
                DirectoryInfo dir = new DirectoryInfo(path);

                FileInfo[] files = dir.GetFiles();
                var sfiles = from file in files select file.Name;

                this.Path = dir.FullName + '\\';
                this.Files = sfiles.ToArray();
            }
            catch
            {
                this.Path = String.Empty;
                this.Files = new String[0];
            }
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsPath(String path, String[] files)
        {
            this.Path = path;
            if (!this.Path.EndsWith("\\"))
                this.Path += '\\';

            this.Files = new String[files.Length];
            for (int i = 0; i < this.Files.Length; i++)
                this.Files[i] = files[i];

            Array.Sort(this.Files);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public WbfsPath(WbfsPath path)
        {
            this.Path = path.Path;

            this.Files = new String[path.Files.Length];
            for (int i = 0; i < this.Files.Length; i++)
                this.Files[i] = path.Files[i];
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public Boolean Contains(String file)
        {
            String path = System.IO.Path.GetDirectoryName(file) + '\\';
            if (String.CompareOrdinal(path, this.Path) != 0) return false;

            file = System.IO.Path.GetFileName(file);
            return ContainsFileName(file);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public Boolean ContainsFileName(String filename)
        {
            int mid;
            int from = 0;
            int to = this.Files.Length - 1;

            do
            {
                mid = (int)(from + to) / 2;
                int cmp = String.CompareOrdinal(filename, this.Files[mid]);
                if (cmp == 0) return true;

                else if (cmp > 0) from = mid + 1;
                else to = mid - 1;
            }
            while (from <= to);

            return false;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public Boolean ContainsAny(String[] files)
        {
            for (int i = 0; i < files.Length; i++)
            {
                if (Contains(files[i]))
                    return true;
            }

            return false;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public Boolean ContainsAnyFileName(String[] filenames)
        {
            for (int i = 0; i < filenames.Length; i++)
            {
                if (ContainsFileName(filenames[i]))
                    return true;
            }

            return false;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public String[] ToArray()
        {
            String[] array = new String[this.Files.Length];
            for (int i = 0; i < this.Files.Length; i++)
                array[i] = this[i];
            
            return array;
        }
    }
}
